import { defineStore } from 'pinia'
import { StatusEnum } from '@/enums/httpEnum'
import {
  getDirectoryList,
  addManagementFile,
  queryDeptList,
  DeptUserList,
  updateManagementRequest
} from '@/api/directory'
import { message as Message } from 'ant-design-vue'
import { BasicResponseModel } from '@/api/model/basicModel'
import {
  DirectoryListParams,
  AddDirmanagementParams,
  UpdateManagementParams,
  SponsorDirectoryFlowParams
} from '@/api/model/directoryModel'

export const useDirectoryStore = defineStore({
  id: 'app-directory',
  state: () => ({
    directoryList: <BasicResponseModel>{},
    managementFile: <BasicResponseModel>{},
    deptUserLis: [],
    deptList: []
  }),
  getters: {},
  actions: {
    // 获取目录列表
    async getDirectoryList(params: DirectoryListParams) {
      const res = await getDirectoryList(params)
      const { status, message } = res
      if (status === StatusEnum.ERROR) {
        Message.error(message)
      }
      if (status === StatusEnum.SUCCESS) {
        this.directoryList = res
      }
      return this.directoryList // 返回 newValue || oldValue
    },

    // 新增目录
    async addManagementFile(params: AddDirmanagementParams) {
      const res = await addManagementFile(params)
      const { status, message } = res
      if (status === StatusEnum.ERROR) {
        Message.error(message)
      }
      if (status === StatusEnum.SUCCESS) {
        this.managementFile = res
      }
      return this.managementFile // 返回 newValue || oldValue
    },
    // 查询所有部门
    async queryDeptList() {
      this.deptList = await queryDeptList()
      return this.deptList
    },
    // 查询所有部门下的所有用户信息
    async DeptUserList() {
      this.deptUserLis = await DeptUserList()
      return this.deptUserList
    },
    // 修改目录
    async updateManagementRequest(params: UpdateManagementParams) {
      const res = await updateManagementRequest(params)
      const { status, message } = res
      if (status === StatusEnum.ERROR) {
        Message.error(message)
      }
      if (status === StatusEnum.SUCCESS) {
        this.directoryList = res
      }
      return this.directoryList // 返回 newValue || oldValue
    }
  }
})
